package com.atguigu.educms.service.impl;

import com.atguigu.common.cron.bean.PageListBean;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.mapper.CrmBannerMapper;
import com.atguigu.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author Panghl
 * @since 2021-01-31
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

    @Override
    public PageListBean pageBanner(Long page, Long limit, CrmBanner crmBanner) {
        Page<CrmBanner> iPage = new Page<>(page, limit);
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(crmBanner.getTitle()), "title", crmBanner.getTitle());
        baseMapper.selectPage(iPage, wrapper);
        return new PageListBean((int) iPage.getTotal(), iPage.getRecords());
    }

    @Cacheable(key = "'selectIndexList'", value = "banner")
    @Override
    public List<CrmBanner> getAllBanner() {
        QueryWrapper<CrmBanner> qw = new QueryWrapper<>();
        qw.orderByDesc("id")
                .last("limit 2");
        return baseMapper.selectList(qw);
    }
}
